﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Text;
using DBUtility;
using System.Data;
using System.Data.OracleClient;

namespace TierApplication 
{
    partial class DeptDao
    {
        public bool CreateDept(DeptInfo info)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("    Insert Into P_Dept ");
            sql.Append("        ( deptname, parentid, remark) ");
            sql.Append("    Values( ");
            sql.Append("         '" + info.DeptName + "',");
            sql.Append("         '" + info.ParentId + "',");
            sql.Append("         '" + info.Remark + "'");
            sql.Append("    ) ");
            return DbHelperOra.ExecuteSql(sql.ToString()) > 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool ModifyDept(DeptInfo info)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" Update P_Dept ");
            sql.Append("    Set deptname = '" + info.DeptName + "',");
            sql.Append("        parentid = '" + info.ParentId + "', ");
            sql.Append("        remark = '" + info.Remark + "' ");
            sql.Append("   Where deptid = '" + info.DeptId + "'");
            return DbHelperOra.ExecuteSql(sql.ToString()) > 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public bool DeleteDept(string DeptId)
        {
            string sql = "Delete P_Dept Where deptid = '" + DeptId + "'";
            return DbHelperOra.ExecuteSql(sql) > 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="MoudleId"></param>
        /// <returns></returns>
        public DeptInfo GetDeptInfo(string DeptId)
        {
            DeptInfo info = new DeptInfo();
            string sql = "Select deptid, deptname, nvl(parentid,'0') parentid, remark From P_Dept where deptid='" + DeptId + "'";
            using (DbDataReader reader = DbHelperOra.ExecuteReader(sql))
            {
                if (reader.Read())
                {
                    info.DeptId = (reader.IsDBNull(reader.GetOrdinal("deptid"))) ? "0" : reader["deptid"].ToString();
                    info.DeptName = (reader.IsDBNull(reader.GetOrdinal("deptname"))) ? "" : reader["deptname"].ToString();
                    info.ParentId = (reader.IsDBNull(reader.GetOrdinal("parentid"))) ? "0" : reader["parentid"].ToString();
                    info.Remark = (reader.IsDBNull(reader.GetOrdinal("remark"))) ? "" : reader["remark"].ToString();
                }
            }
            return info;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<DeptInfo> GetDeptAll()
        {
            List<DeptInfo> list = new List<DeptInfo>();
            DeptInfo info = null;
            string sql = "Select deptid, deptname, nvl(parentid,'0') parentid, remark From P_Dept";
            using (DbDataReader reader = DbHelperOra.ExecuteReader(sql))
            {
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        info = new DeptInfo();
                        info.DeptId = (reader.IsDBNull(reader.GetOrdinal("deptid"))) ? "0" : reader["deptid"].ToString();
                        info.DeptName = (reader.IsDBNull(reader.GetOrdinal("deptname"))) ? "" : reader["deptname"].ToString();
                        info.ParentId = (reader.IsDBNull(reader.GetOrdinal("parentid"))) ? "0" : reader["parentid"].ToString();
                        info.Remark = (reader.IsDBNull(reader.GetOrdinal("remark"))) ? "" : reader["remark"].ToString();
                        list.Add(info);
                    }
                }
            }
            return list;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataTable GetDeptTable()
        {
            string sql = "Select deptid, deptname, nvl(parentid,'0') parentid, remark From P_Dept";
            return DbHelperOra.Query(sql).Tables[0];
        }
    }
}